<?php

namespace app\admin\logic;

use nx\Data;
use think\Cache;
use app\admin\model\Activity as Activity_model;

/**
 * Class Activity 活动操作数据
 * @package app\admin\logic
 * @author: Huang
 */
class Activity
{
    public $Activity_model = '';

    /**获取模型
     * @return Activity_model|string
     */
    public function getModel()
    {
        if (empty($this->Activity_model)) {
            $this->Activity_model = new Activity_model();
        }
        return $this->Activity_model;
    }

    /**获取所有的活动活动（供获取器使用）
     * @return array|false|mixed|\PDOStatement|string|\think\Collection
     */
    public static function ActivitysToText()
    {
        $Activitys = self::getAllActivitys();
        $Activitys = collect($Activitys)->column('name', 'id');
        return $Activitys;
    }

    /**将所有活动进行树形排列
     * @return array|false|mixed|\PDOStatement|string|\think\Collection
     */
    public function getAllActivityToTree()
    {
        $Activitys = $this->getAllActivitys();
        $Activitys = Data::tree($Activitys, 'title', 'id', 'pid');
        return $Activitys;
    }

    /**获得所有的活动活动（从缓存中）
     * @return false|mixed|\PDOStatement|string|\think\Collection   返回数组
     */
    public function getAllActivitys()
    {
        $Activitys = Cache::get('allActivitys');
        if (!empty($Activitys)) return $Activitys;
        $Activitys = Activity_model::all(['status'=>1]);
        if (empty($Activitys)) return $Activitys;
        $Activitys = collection($Activitys)->append(['status_text'])->toArray();
        Cache::set('allActivitys', $Activitys);
        return $Activitys;
    }


    /**
     * 获取活动 select 数据
     * @return array
     * @author: Huang
     */
    public function getAllText()
    {
        $Activitys = $this->getAllActivitys();
        $companys = collect($Activitys)->column('title', 'id');
        return $companys;
    }

    /**
     * 获取某活动的所有下属活动
     * @param $Activity_id
     * @return array|mixed
     */
    public function getNextActivitys($Activity_id)
    {
        $data = $this->getAllActivitys();
        $Activitys = Data::channelList($data, $Activity_id, '— ', 'id', 'pid');
        return $Activitys;
    }


    public function addActivity($data)
    {
        if (empty($data)) {
            return ['result' => false, 'msg' => '数据丢失'];
        }
        if (!empty($data['code'])) {
            $data['code'] = strtoupper(trim($data['code']));
        }
        $result = $this->getModel()->validate(true)->save($data);
        if (false === $result) {
            return ['result' => false, 'msg' => $this->getModel()->getError()];
        } else {
            Cache::rm('allActivitys');
            return ['result' => true, 'msg' => '活动添加成功'];
        }
    }

    public function editActivity($data)
    {
        if (empty($data['id'])) {
            return ['result' => false, 'msg' => 'ID丢失'];
        }
        isset($data['status']) ?: $data['status'] = 1;
        $result = $this->getModel()->allowField(true)->validate(true)->isUpdate(true)->save($data);
        if (false === $result) {
            return ['result' => false, 'msg' => $this->getModel()->getError()];
        } else {
            Cache::rm('allActivitys');
            return ['result' => true, 'msg' => '活动修改成功'];
        }
    }

    public function delActivity($id)
    {
        if (empty($id)) {
            return ['result' => false, 'msg' => '请选择要操作的数据'];
        }
        if (Activity_model::get($id)) {
            if (Activity_model::destroy($id)) {
                Cache::rm('allActivitys');
                return ['result' => true, 'msg' => '删除成功'];
            } else {
                return ['result' => false, 'msg' => '删除失败'];
            }
        } else {
            return ['result' => false, 'msg' => '要删除的数据不存在'];
        }
    }


}
